package com.starrin.trashtalk;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Created by larsstarrin on 11/10/14.
 */
@WebListener
public class PersistenceListener implements ServletContextListener {

        private static EntityManagerFactory emf;

        @Override
        public void contextInitialized(ServletContextEvent event) {
            emf = Persistence.createEntityManagerFactory("playtennis.jpa");
        }

        @Override
        public void contextDestroyed(ServletContextEvent event) {
            emf.close();
        }

        public static EntityManager createEntityManager() {
            if (emf == null) {
                throw new IllegalStateException("Context is not initialized yet.");
            }

            return emf.createEntityManager();
        }

}
